package com.fzj.example.io;

import java.io.*;
import java.util.Scanner;

/**
 * ClassName: ConsoleDemo
 * date: 2019/11/7 14:25
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class ConsoleDemo {
    public static void main(String[] args) throws IOException {
        /********************读取单个字符***************************/
        //字节转字符流，从而有reader读取单个字符
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        BufferedReader reader2=new BufferedReader(new InputStreamReader(System.in));
        //char与int可以强转操作
/*        char consoleChar;
        do{
            consoleChar= (char)reader.read();
            if(consoleChar!=' ' && consoleChar!='\n'){
                System.out.println("console input conent:"+consoleChar);
            }

        }while (consoleChar!='q');*/


        PrintStream printStream=new PrintStream(System.out);
            char consoleChar;
            do{
                consoleChar= (char)reader.read();
                if(consoleChar!=' ' && consoleChar!='\n'){
                    printStream.write(consoleChar);
                    printStream.flush();
                }
                printStream.write('\n');
                printStream.flush();

        }while (consoleChar!='q');



        //流的特点，即像水一样，取水喝了，就用了，流不可重复读
        /********************读取行***************************/
/*
        String consoleLine;
        do{
            consoleLine= reader2.readLine();
            if(!consoleLine.equals("") && !consoleLine.equals("\n")){
                System.out.println("console input conent:"+consoleLine);
            }

        }while (!consoleLine.equals("quit"));
*/

        /********************读取行***************************/
/*
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNext()){
            //autoFlush有时候不管用
            PrintWriter printWriter=new PrintWriter(System.out,true);
            printWriter.write("打印流:"+scanner.nextLine());
            printWriter.flush();
        }
*/



        //程序里面打开的文件IO资源不属于内存的资源，垃圾回收机制无法回收该资源，所以应该显示的关闭打开的IO资源

        //关闭流
    }
}
